//
//  SearchresultTableViewController.m
//  民族文化宫
//
//  Created by kobehjk on 14-7-5.
//  Copyright (c) 2014年 WANG CUNRUI. All rights reserved.
//

#import "SearchresultTableViewController.h"
#import "PinYin4Objc.h"
#import "MZSection.h"
#import "SearchViewController.h"
#import "MZMetaDataTool.h"



@interface SearchresultTableViewController ()
{
NSMutableArray *_resultCities; // 放着所有搜索到的城市
}
@end

@implementation SearchresultTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization

    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _resultCities = [NSMutableArray array];

    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)setSearchText:(NSString *)searchText
{
    _searchText = searchText;
    
    // 1.清除之前的搜索结果
    [_resultCities removeAllObjects];
    
    // 2.筛选
    HanyuPinyinOutputFormat *fmt = [[HanyuPinyinOutputFormat alloc] init];
    fmt.caseType = CaseTypeUppercase;
    fmt.toneType = ToneTypeWithoutTone;
    fmt.vCharType = VCharTypeWithUUnicode;
    NSDictionary *cities = [MZMetaDataTool sharedMZMetaDataTool].allnature;
    //[TGMetaDataTool sharedTGMetaDataTool].totalCities;
    [cities enumerateKeysAndObjectsUsingBlock:^(NSString *key,  NSString *obj, BOOL *stop) {
        //NSLog(@"weq%@",obj.name);
        // SHI#JIA#ZHUANG
        // 1.拼音
        NSString *pinyin = [PinyinHelper toHanyuPinyinStringWithNSString:obj withHanyuPinyinOutputFormat:fmt withNSString:@"#"];
        
        // 2.拼音首字母
        NSArray *words = [pinyin componentsSeparatedByString:@"#"];
        NSMutableString *pinyinHeader = [NSMutableString string];
        for (NSString *word in words) {
            [pinyinHeader appendString:[word substringToIndex:1]];
        }
        
        /*
         补充：这里少加一行代码
         */
        pinyin = [pinyin stringByReplacingOccurrencesOfString:@"#" withString:@""];
        
        // 3.城市名中包含了搜索条件
        // 拼音中包含了搜索条件
        // 拼音首字母中包含了搜索条件
        if (([obj rangeOfString:searchText].length != 0) ||
            ([pinyin rangeOfString:searchText.uppercaseString].length != 0)||
            ([pinyinHeader rangeOfString:searchText.uppercaseString].length != 0))
        {
            // 说明城市名中包含了搜索条件
            [_resultCities addObject:obj];
            NSLog(@"result = %@",_resultCities);
        }
    }];
    
    // 3.刷新表格
    [self.tableView reloadData];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"共%d个搜索结果", _resultCities.count];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _resultCities.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
//    MZSection *city = _resultCities[indexPath.row];
    cell.textLabel.text = _resultCities[indexPath.row];
    NSLog(@"log = %@",_resultCities[indexPath.row]);
    
    return cell;
    [self resignFirstResponder];
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [MZMetaDataTool sharedMZMetaDataTool].currentCity = _resultCities[indexPath.row];
    _detailController = [[MZdetailViewController alloc] init];
    _detailController.minzu = _resultCities[indexPath.row];
    [self.navigationController pushViewController:_detailController animated:YES];
    [self resignFirstResponder];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}


@end
